namespace AltNetUK.Models
{
    using System;
    using System.Globalization;
    using System.Net.Mail;
    using System.Text;
    using SimpleWebServices.Data;

    public sealed class AdministrationEmail
    {
        private Registration _registration;

        public AdministrationEmail(Registration registration)
        {
            this.Registration = registration;
        }

        public Registration Registration
        {
            get
            {
                return this._registration;
            }

            set
            {
                if (null == value)
                {
                    throw new ArgumentNullException("value");
                }

                this._registration = value;
            }
        }

        public void Send()
        {
            MailMessage email = new MailMessage();
            email.To.Add(new MailAddress("alan.dean@thoughtmail.net"));
            email.From = new MailAddress("no-reply@altnetuk.com", "Alt.Net UK Conference");
            email.IsBodyHtml = false;

            email.Subject = string.Format(CultureInfo.InvariantCulture, "[Alt.Net UK] {0} registered", this.Registration.Name);

            #region Body

            StringBuilder body = new StringBuilder();

            body.AppendLine("http://www.altnetuk.com/registration.en.html/" + (this.Registration as IPersistable).Unique.ToString());

            body.AppendLine(string.Empty);
            body.AppendLine("[Name]");
            body.AppendLine(this.Registration.Name);

            body.AppendLine(string.Empty);
            body.AppendLine("[Twitter]");
            if (string.IsNullOrEmpty(this.Registration.Twitter))
            {
                body.AppendLine("not provided");
            }
            else
            {
                body.AppendLine("http://twitter.com/" + this.Registration.Twitter);
            }

            body.AppendLine(string.Empty);
            body.AppendLine("[Email]");
            body.AppendLine(this.Registration.Email);

            body.AppendLine(string.Empty);
            body.AppendLine("[altnetbeers]");
            body.AppendLine(this.Registration.AltNetBeers ? "Attendance requested" : "Not requested");

            body.AppendLine(string.Empty);
            body.AppendLine("[openspacecode]");
            body.AppendLine(this.Registration.OpenSpaceCoding ? "Attendance requested" : "Not requested");

            body.AppendLine(string.Empty);
            body.AppendLine("[altnetconf]");
            body.AppendLine(this.Registration.AltNetConference ? "Attendance requested" : "Not requested");

            body.AppendLine(string.Empty);
            body.AppendLine("[Message]");
            body.AppendLine(this.Registration.Message);

            email.Body = body.ToString();

            #endregion

            new SmtpClient().Send(email);
        }
    }
}